package com.efast.cafe.portal.controller.api;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.PortalApiObjectDto;
import com.efast.cafe.portal.bean.api.PortalApiSystemBean;
import com.efast.cafe.portal.bean.api.PortalApiSytemQueryBean;
import com.efast.cafe.portal.service.api.IPortalApiSystemService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.support.RequestContext;

/**
 * <p>
 * Controller
 * </p>
 * <p>
 * Table: portal_api_system - 所属系统定义
 * </p>
 *
 * @since 2017-08-08 11:45:57
 */
@Controller
@RequestMapping("/frame_center/api")
public class PortalApiSystemController extends AbstractController {

	@Autowired
	private IPortalApiSystemService portalApiSystemService;

	

	@RequestMapping("/system/list")
	public @ResponseBody DataTableVo<PortalApiObjectDto> queryApiSystemList(
			@RequestBody PortalApiSytemQueryBean queryBean, HttpServletRequest request) {
		queryBean = portalApiSystemService.queryApiSystemList(queryBean);
		return outPageJSONArray_bootstrapDatatable(queryBean);
	}

	/**
	 * 在项目下新增一个系统
	 * 
	 * @param portalApiSystemBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/system/save")
	public @ResponseBody ResultBean insertApiSystem(@RequestBody PortalApiSystemBean portalApiSystemBean,
			HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			// 判断版本号是否重复
			result.setSuccess(true);
			Integer id = portalApiSystemService.insertApiSystem(portalApiSystemBean);
			result.setObject(id.toString());
			result.setMessage(ADD_SUCCESS_TIP);
		} catch (Exception e) {
			result.setSuccess(false);
			e.printStackTrace();
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			}
		}
		return result;
	}

	/**
	 * 修改系统信息
	 * 
	 * @param portalApiSystemBean
	 * @param request
	 * @return
	 */
	@RequestMapping("/system/update")
	public @ResponseBody ResultBean updateApiSystem(@RequestBody PortalApiSystemBean portalApiSystemBean,
			HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			portalApiSystemBean.setStatus((byte) 2);
			portalApiSystemBean.setId(portalApiSystemBean.getSystemId());
			result.setSuccess(true);
			portalApiSystemService.updateApiSystem(portalApiSystemBean);
			// result.setMessage(context.getMessage(EDIT_SUCCESS_TIP));
			result.setMessage(EDIT_SUCCESS_TIP);

		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			// result.setMessage(context.getMessage(EDIT_FAILURE_TIP));
		}
		return result;
	}

	/**
	 * 删除一个系统
	 * 
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping("/system/delete")
	public @ResponseBody ResultBean deleteApiSystem(@RequestBody Map<String,Integer> map,
			HttpServletRequest request) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			Integer id=map.get("id");
			portalApiSystemService.deleteApiSystem(id);
			// result.setMessage(context.getMessage(DEL_SUCCESS_TIP));
			result.setMessage(DEL_SUCCESS_TIP);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if(e instanceof ServiceException){
				result.setMessage(e.getMessage());
			}
			// result.setMessage(context.getMessage(DEL_FAILURE_TIP));
		}
		return result;
	}

	@RequestMapping("/system/publish")
	public @ResponseBody ResultBean publishApiSystem(HttpServletRequest request,
			@RequestBody PortalApiSystemBean portalApiSystemBean) {
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			portalApiSystemBean.setId(portalApiSystemBean.getSystemId());
			portalApiSystemService.publishApiSystem(portalApiSystemBean);
			result.setMessage(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			if (e instanceof ServiceException) {
				result.setMessage(e.getMessage());
			} else {
				result.setMessage(FAILURE);
			}
		}
		return result;
	}

	@RequestMapping("/system/upgrade")
	public @ResponseBody ResultBean upgradeApiSystem(HttpServletRequest request,
			@RequestBody PortalApiSystemBean portalApiSystemBean) {
	//	RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			portalApiSystemBean.setId(portalApiSystemBean.getSystemId());
			portalApiSystemService.upgradeApiSystem(portalApiSystemBean);
			// result.setMessage(context.getMessage(DEL_SUCCESS_TIP));
			result.setMessage(SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(FAILURE);
			// result.setMessage(context.getMessage(DEL_FAILURE_TIP));
		}
		return result;
	}

	/**
	 * 查询指定系统的所有版本信息
	 * 
	 * @param request
	 * @param projectId
	 * @param systemId
	 * @return ResultBean
	 * @Author Administrator
	 * @Date 2017年8月22日
	 */
	@RequestMapping("/system/versions")
	public @ResponseBody ResultBean queryApiSystemAllVersions(HttpServletRequest request,@RequestBody Map<String,Object> map) {
		//RequestContext context = new RequestContext(request);
		ResultBean result = new ResultBean();
		try {
			result.setSuccess(true);
			Integer projectId = (Integer)map.get("projectId");
			Integer systemId = (Integer)map.get("systemId");
			Integer status = (Integer)map.get("status");
			List<PortalApiSystemBean> resultList = portalApiSystemService.queryApiSystemOtherVersions(projectId,
					systemId, status);
			result.setMessage(SUCCESS);
			result.setObject(resultList);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(FAILURE);
		}
		return result;
	}
}